package com.arvin.imagepicker.widget;

import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.StyleRes;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;

import com.arvin.imagepicker.R;
import com.arvin.imagepicker.bean.ImageItem;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;



/**
 * Created by arvin on 2017/10/27.
 */

public class CustomImgDialog extends AlertDialog {
    private Context mContext;
    private MineAdapter mMineAdapter;
    private List<View> views=new ArrayList<>();
    private List<ImageItem> mPics;
    private boolean mCanEdit;
    private int startPosition;
    private int mCurrentPosition;
    private OnDeleteListener mOnDeleteListener;
    private OnMultiDeleteListener mOnMultiDeleteListener;

    public CustomImgDialog(Context context, @StyleRes int themeResId,List<ImageItem> list,boolean canEdit,int position) {
        super(context, themeResId);
        mContext=context;
        mPics=list;
        mCanEdit=canEdit;
        startPosition=position;
        mCurrentPosition=startPosition;
    }
    public void setOnDeleteListener(OnDeleteListener listener)
    {
        mOnDeleteListener=listener;
    }

    public void setOnMultiDeleteListener(OnMultiDeleteListener onMultiDeleteListener) {
        mOnMultiDeleteListener = onMultiDeleteListener;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        View decorView = getWindow().getDecorView();
//        int option= View.SYSTEM_UI_FLAG_FULLSCREEN;
//        decorView.setSystemUiVisibility(option);

        final View view = LayoutInflater.from(mContext).inflate(R.layout.view_browsepics, null);
        setContentView(view);
        PhotoViewInViewPager vp= (PhotoViewInViewPager) view.findViewById(R.id.vp);
        View delete = view.findViewById(R.id.iv_del);
        ImageView mImgBack = view.findViewById(R.id.img_back);
        mImgBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        if(mPics!=null&&mPics.size()>0)
        {
            if(mCanEdit)
            {
                delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        ImageItem item = mPics.get(mCurrentPosition);
                        views.remove(mCurrentPosition);
                        mPics.remove(mCurrentPosition);
                        if(mOnDeleteListener!=null)
                        {
                            mOnDeleteListener.onDelete();
                        }
                        if(mOnMultiDeleteListener!=null)
                        {
                            mOnMultiDeleteListener.onDelete(item);
                        }

                        if(mPics.size()==0)
                        {
                            dismiss();
                        }else {
                            mMineAdapter.notifyDataSetChanged();
                        }
                    }
                });
            }else
            {
                delete.setVisibility(View.GONE);
            }

            for(ImageItem item:mPics)
            {
                View inflate = LayoutInflater.from(mContext).inflate(R.layout.item_browsepics, null);
                views.add(inflate);
            }
            mMineAdapter = new MineAdapter();
            vp.setAdapter(mMineAdapter);
            vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
                @Override
                public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

                }

                @Override
                public void onPageSelected(int position) {
                    mCurrentPosition=position;
                }

                @Override
                public void onPageScrollStateChanged(int state) {

                }
            });
            vp.setCurrentItem(startPosition);
        }


    }

    RequestOptions options = new RequestOptions()
            .error(R.drawable.ic_holder_w)
            .placeholder(R.drawable.ic_holder_w);

    class MineAdapter extends PagerAdapter
    {

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = views.get(position);
            TouchImageView pv= (TouchImageView) view.findViewById(R.id.pv);
            ImageItem imageItem = mPics.get(position);
            if(!TextUtils.isEmpty(imageItem.path))
            {
                Glide.with(mContext).load(new File(imageItem.path)).apply(options).into(pv);
            }else if(!TextUtils.isEmpty(imageItem.url))
            {
                Glide.with(mContext).load(imageItem.url).apply(options).into(pv);
            }
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View)object);
        }


        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view==object;
        }

        @Override
        public int getItemPosition(Object object) {
            return POSITION_NONE;
        }
    }

    public interface OnDeleteListener
    {
        void onDelete();
    }

    public interface OnMultiDeleteListener
    {
        void onDelete(ImageItem imageItem);
    }

}
